package com.ticket.web.controller.yanchu;

import com.google.common.base.Splitter;
import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.system.domain.YanchuPerform;
import com.ticket.system.domain.vo.PerFormAppVO;
import com.ticket.system.service.IYanchuPerformService;
import com.ticket.system.service.IYanchuSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 演出Controller
 * 
 * @author ruoyi
 * @date 2023-09-27
 */
@Api(description = "演出管理")
@RestController
@RequestMapping("/system/perform")
public class YanchuPerformController extends BaseController
{
    @Autowired
    private IYanchuPerformService yanchuPerformService;
    @Autowired
    private IYanchuSessionService yanchuSessionService;


    @ApiOperation("查询演出列表pc")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operaId",value = "剧院id",required = true,dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type",value = "0:待发布;1:待销售;2:销售中;3:销售结束",dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "keyName",value = "演出名称模糊匹配",dataType = "String",dataTypeClass = String.class)
    })
    @PreAuthorize("@ss.hasPermi('system:perform:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Long operaId,Integer type,String keyName)
    {
        YanchuPerform yanchuPerform = new YanchuPerform();
        yanchuPerform.setOperaId(operaId);
        if (StringUtils.isNotEmpty(keyName)){
            yanchuPerform.setPerformName(keyName);
        }
        if(null != type){
            yanchuPerform.setType(type);
        }
        startPage();
        List<YanchuPerform> list = yanchuPerformService.selectYanchuPerformList(yanchuPerform);
        if (CollectionUtils.isNotEmpty(list)) {
            for (YanchuPerform perform : list) {
                if(null==perform.getTicketStatistics()){
                    perform.setType(0);
                    continue;
                }
                if (perform.getTicketStatistics().getSelledSession()>0){
                    perform.setType(2);
                }else if(perform.getTicketStatistics().getNoSelledSession()>0 && perform.getTicketStatistics().getSelledSession()==0){
                    perform.setType(1);
                }else if(perform.getTicketStatistics().getEndNum()>0 && perform.getTicketStatistics().getSelledSession()==0){
                    perform.setType(3);
                }else{
                    perform.setType(0);
                }
            }
        }
        return getDataTable(list);
    }

    @ApiOperation("小程序端演出列表搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "1:当前热门演出;2:即将上映演出;3:全部演出",dataType = "int",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "performType",value = "演出类型的id,全部类型时不传参",dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "operaId",value = "剧院id",dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "startTime",value = "开始时间 yyyy/MM/dd HH:mm:ss",dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime",value = "结束时间 yyyy/MM/dd HH:mm:ss",dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "orderType",value = "1.距离 2.销量 3.低价 4.好评",dataType = "int",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "longitude",value = "经度",dataType = "double",dataTypeClass = Double.class),
            @ApiImplicitParam(name = "latitude",value = "纬度",dataType = "double",dataTypeClass = Double.class)
    })
    @GetMapping("/listPerform/forAllUser")
    public TableDataInfo listPerformQuery(String keyName,Integer type,Long performType,Long operaId,String startTime,String endTime,
                                          Integer orderType,Double longitude,Double latitude)
    {
        List<String> keys = Lists.newArrayList();
        if(StringUtils.isNotEmpty(keyName)){
            keys = Splitter.on(" ").trimResults().omitEmptyStrings().splitToList(keyName);
        }
        List<PerFormAppVO> performAppVO = yanchuPerformService.selectPerformQueryList(keys, performType, operaId, startTime,endTime,
                type,orderType,longitude,latitude);
        return getDataTable(performAppVO);
    }

    @ApiOperation("查询演出pc端统计列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operaIds",value = "剧院id,多个,号分隔",required = true,dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "status",value = "0初始化 1已发布 2暂停销售 3取消 4销售中 售票结束5 ,多个,号分隔",dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "performTypes",value = "演出类型,多个,号分隔",dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "keyName",value = "演出名称模糊匹配",dataType = "String",dataTypeClass = String.class),
    })
//    @PreAuthorize("@ss.hasPermi('system:perform:listStatics')")
    @GetMapping("/listPerformStatic")
    public TableDataInfo listPerformStatic(String operaIds,String status,String performTypes,String keyName)
    {
        //startPage();
        List<YanchuPerform> list = yanchuPerformService.selectPerformStaticsList(operaIds,status,performTypes,keyName);
        return getDataTable(list);
    }

    @ApiOperation("小程序端演出详情")
    @ApiImplicitParam(name = "performId", value = "演出编码", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/detailPerform/forAllUser")
    public AjaxResult detailPerform(@RequestParam(name = "performId") Long performId)
    {
        return AjaxResult.success(yanchuPerformService.selectMobYanchuPerformById(performId));
    }

    @ApiOperation("海报新增  选择演出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyName",value = "模糊匹配字段",dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "operaId",value = "剧院id",dataType = "Long",required = true,dataTypeClass = Long.class),
    })
    @PostMapping("/getPerformByPosters")
    @Log(title = "海报新增", businessType = BusinessType.OTHER)
    public AjaxResult getPerformByPosters(@RequestParam Long operaId,String keyName){
        return success(yanchuPerformService.getPerformByPosters(operaId,keyName));
    }

    /**
     * 导出演出列表
     */
    @PreAuthorize("@ss.hasPermi('system:perform:export')")
    @Log(title = "演出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuPerform yanchuPerform)
    {
        List<YanchuPerform> list = yanchuPerformService.selectYanchuPerformList(yanchuPerform);
        ExcelUtil<YanchuPerform> util = new ExcelUtil<YanchuPerform>(YanchuPerform.class);
        util.exportExcel(response, list, "演出数据");
    }

    @ApiOperation("获取演出详细信息")
    @PreAuthorize("@ss.hasPermi('system:perform:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuPerformService.selectYanchuPerformById(id));
    }


    @PreAuthorize("@ss.hasPermi('system:perform:add')")
    @Log(title = "新增演出", businessType = BusinessType.INSERT)
    @ApiOperation("新增演出")
    @ApiImplicitParam(name = "yanchuPerform", value = "演出对象", dataType = "YanchuPerform", dataTypeClass = YanchuPerform.class)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated YanchuPerform yanchuPerform)
    {
        yanchuPerform.setOperaId(getOperaId());
        return toAjax(yanchuPerformService.insertYanchuPerform(yanchuPerform));
    }

    @ApiOperation("修改演出")
    @PreAuthorize("@ss.hasPermi('system:perform:edit')")
    @Log(title = "演出", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated YanchuPerform yanchuPerform)
    {
        yanchuPerform.setOperaId(getOperaId());
        return toAjax(yanchuPerformService.updateYanchuPerform(yanchuPerform));
    }

    @ApiOperation("删除演出")
    @PreAuthorize("@ss.hasPermi('system:perform:remove')")
    @Log(title = "删除演出", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(yanchuPerformService.deleteYanchuPerformById(id));
    }
}
